package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.Article;
import com.example.demo.entity.ArticleImg;
import com.example.demo.entity.DTO.ArticleAndUserDTO;
import com.example.demo.entity.DTO.ArticleSelectDTO;
import com.example.demo.entity.VO.ArticlePublishVO;
import com.example.demo.entity.VO.ArticleSelectVO;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleImgService;
import com.example.demo.service.IArticleService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Api(tags = "文章接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArticleImgService articleImgService;
    @Autowired
    private IUserService userService;


    /**
     * 友圈发布帖子
     * @param articlePublishVO
     * @return
     */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "友圈发布功能")
    @PostMapping("/publish")
    @SaCheckLogin
    public Result<String> publish(@RequestBody ArticlePublishVO articlePublishVO) {

        Article article = new Article();
        article.setUserId(StpUtil.getLoginIdAsInt());
        if (userService.getById(article.getUserId()) == null) {
            return Result.error().code(99403).msg("无该用户");
        }
        ArrayList<ArticleImg> articleImgs = new ArrayList<>();
        BeanUtil.copyProperties(articlePublishVO, article);

        int publish = articleService.publish(article);

        if (articlePublishVO.getImgPath() != null) {
            for (String s : articlePublishVO.getImgPath()) {
                ArticleImg articleImg = new ArticleImg();
                articleImg.setImgPath(s);
                articleImg.setArticleId(publish);
                articleImgs.add(articleImg);
            }
        }
        boolean b = articleImgService.addArticleImg(articleImgs);
        if (!b) {
            return Result.error().msg("发布失败");
        }
        return Result.success().msg("发布成功");
    }

    /**
     * 根据内容查找所有接口
     * @param articleSelectVO
     * @return
     */

    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "根据内容查找所有接口")
    @PostMapping("/findAllByCriteria")
    public Result<List<ArticleSelectDTO>> findAllByCriteria(@RequestBody ArticleSelectVO articleSelectVO) {
        Article article = new Article();
        BeanUtil.copyProperties(articleSelectVO, article);

        return Result.success().msg("查询成功").data(articleService.findAllByCriteria(article,articleSelectVO.getUserName()));
    }


    @ApiOperationSupport(author = "Mr.wyy")
    @ApiOperation(value = "根据userId查询对应用户写的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "组件id",required = true)
    })
    @GetMapping("/selectArticleAndUserById")
    public Result<List<ArticleAndUserDTO>> selectArticleAndUserById(@RequestParam Integer userId) {


        return Result.success(articleService.selectArticleAndUserById(userId)).msg("查询成功");
    }




    /**
     * 模糊查询所有
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "根据内容模糊查询文章")
    //@SaCheckLogin  测试不开登录
    @GetMapping("/selectByArticle")
    public Result<List<ArticleAndUserDTO>> findAllArticle(String article) {
        List<ArticleAndUserDTO> byArticle = articleService.findByArticle(article);
        if (byArticle == null || byArticle.isEmpty()) {
            return Result.error().msg("未查询到相关文章");
        }
        return Result.success(byArticle).msg("查询成功");
    }


//    /**
//     * 查询所有接口
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "查询文章表所有数据")
//    @GetMapping("/findAll")
//    public Result<List<Article>> findAll() {
//        return Result.success(articleService.list()).msg("查询成功");
//    }
//
//    /**
//     * 根据id查询数据接口
//     * @param id
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "根据id查询数据接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "组件id",required = true)
//    })
//    @GetMapping("/{id}")
//    public Result<Article> findOne(@PathVariable Integer id) {
//        return Result.success(articleService.getById(id)).msg("查询成功");
//    }
//
//    /**
//     * 分页查询接口
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "分页查询接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum",value = "页码",example="1",required = true),
//            @ApiImplicitParam(name = "pageSize",value = "每页多少条",example="10",required = true)
//    })
//    @GetMapping("/page")
//    public Result<MyPage<Article>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
//        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
//        return Result.success().data(articleService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("查询成功");
//    }
//
//    /**
//    * 新增
//    * @param
//    * @return
//    */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "新增")
//    @PostMapping("/add")
//    //TODO 记得来创建这个VO
//    public Result<String> add() {
//        //TODO VO转Entity
//        //articleService.saveOrUpdate(article);
//        return Result.success().msg("添加成功");
//    }
//
//    /**
//     * 更新接口
//     * @param
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "更新接口")
//    @PutMapping
//    public Result<String> save(@RequestBody Article article) {
//        articleService.saveOrUpdate(article);
//        return Result.success().msg("更新成功");
//    }
//
//    /**
//     * 删除接口
//     * @param id
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "删除接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "组件id",required = true)
//    })
//    @DeleteMapping("/{id}")
//    public Result<String> delete(@PathVariable Integer id) {
//        articleService.removeById(id);
//        return Result.success().msg("删除成功");
//    }
//
//    /**
//     * 批量删除接口
//     * @param ids
//     * @return
//     */
//    @ApiOperationSupport(author = "Mr.Yang")
//    @ApiOperation(value = "批量删除接口")
//    @PostMapping("/del/batch")
//    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
//        articleService.removeByIds(ids);
//        return Result.success().msg("批量删除成功");
//    }
}